package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.Region;
import com.yu.crm.domain.pojo.Shift;
import com.yu.crm.domain.pojo.Ticketinfo;
import com.yu.crm.mapper.ShiftMapper;
import com.yu.crm.service.IShiftService;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zds
 * @since 2021-03-22
 */
@Service
public class ShiftService extends ServiceImpl<ShiftMapper, Shift> implements IShiftService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void deleteByIds(String ids) {
        try {
            String substring = ids.substring(0, ids.length());
            String[] result = substring.split(",");
            for (String s : result) {
                Shift shift = baseMapper.selectById(Long.valueOf(s));
                if (DataHelpers.isNotEmpty(shift)) {
                    shift.setIsDeleted(1);
                    ValidatorHelpers.validateDataSave(baseMapper.updateById(shift));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public Shift infoById(Long id) {
        try {
            return baseMapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public List<Shift> getPage(String stationName, String condition) {
        QueryWrapper<Shift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        if (DataHelpers.isNotEmptyString(stationName)) {
            queryWrapper.eq("station_name", stationName);
        }
        if (DataHelpers.isNotEmptyString(condition)) {
            queryWrapper.and(i -> i.like("station_name", condition)
            );
        }
        return baseMapper.selectList(queryWrapper);
    }

    public IPage<Shift> getPage(Page<Shift> page, String stationName, String condition) {
        QueryWrapper<Shift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        if (DataHelpers.isNotEmptyString(stationName)) {
            queryWrapper.eq("station_name", stationName);
        }
        if (DataHelpers.isNotEmptyString(condition)) {
            queryWrapper.and(i -> i.like("station_name", condition)
            );
        }
        return baseMapper.selectPage(page, queryWrapper);
    }
}
